package com.example.taobao;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.taobao.data.UserDataDpSQLHelp;
import com.example.taobao.data.userdata;
import com.example.taobao.ui.login.LoginActivity;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class UUserMainActivity extends AppCompatActivity {


    private TextView textView1;
    private Button registerBut1;
    private EditText userNameEdt1, passwordEdt1, passwordEdt11, vipNameEdt1, iPhoneEdt1;
    private Spinner addressSpinner1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uuser_main);

        userNameEdt1 = findViewById(R.id.useridXin);
        passwordEdt1 = findViewById(R.id.zhucpasswordXin);
        passwordEdt11 = findViewById(R.id.yespasswordXin);
        vipNameEdt1 = findViewById(R.id.VipnameXin);
        iPhoneEdt1 = findViewById(R.id.iPhoneXin);
        addressSpinner1 = findViewById(R.id.addressSpinnerXin);

        String[] str = new String[]{"北京", "天津", "上海", "重庆", "新疆", "西藏", "宁夏", "内蒙古",
                "广西", "黑龙江", "吉林", "辽宁", "河北", "山东", "江苏", "安徽",
                "浙江", "福建", "广东", "海南", "云南", "贵州", "四川", "湖南",
                "湖北", "河南", "山西", "陕西", "甘肃", "青海", "江西", "台湾", "香港", "澳门"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(UUserMainActivity.this, R.layout.support_simple_spinner_dropdown_item, str);

        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);

        addressSpinner1.setAdapter(adapter);//省份


        registerBut1 = findViewById(R.id.registerButXin);


        registerBut1.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                String username = userNameEdt1.getText().toString();
                String password = passwordEdt1.getText().toString();
                String password1 = passwordEdt11.getText().toString();
                String vipName = vipNameEdt1.getText().toString();
                String iphone = iPhoneEdt1.getText().toString();
                String address = addressSpinner1.getSelectedItem().toString();

                userdata userss = new userdata();
                userss.setUsername(username);
                userss.setPassword(password);
                userss.setVipName(vipName);
                userss.setIphone(iphone);
                userss.setAddress(address);
//                userss.setLoginZt(false);
//                user_data = new userdata(username,password,vipName,iphone,address);//创建用户类加入数据

                if (password.equals(password1)) {

                    BmobQuery<userdata> bmobQuery = new BmobQuery<userdata>();
                    bmobQuery.findObjects(new FindListener<userdata>() {
                        @Override
                        public void done(List<userdata> list, BmobException e) {
                            if (e == null) {
                                int i;
                                Boolean Drl;
                                for (i = 0; i < list.size(); i++) {
                                    Drl = list.get(i).getLoginZt();
                                    if (Drl) {
                                        String obje = list.get(i).getObjectId();

                                        userss.update(obje, new UpdateListener() {

                                            @Override
                                            public void done(BmobException e) {
                                                if (e == null) {
                                                    Toast.makeText(UUserMainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                                    Intent intent = new Intent(UUserMainActivity.this,bottomActivity.class);
                                                    startActivityForResult(intent,4598);

                                                } else {
                                                    Toast.makeText(UUserMainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                                                }
                                            }

                                        });
                                        break;
                                    }

                                }

                            } else{
                                Toast.makeText(UUserMainActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                    });
                }
                else {
                    Toast.makeText(UUserMainActivity.this, "两次密码不一样", Toast.LENGTH_SHORT).show();
                }


            }
        });

    }
}




